package com.qulai.web.controller.basic;

import com.qulai.amazon_us.domain.basic.Product;
import com.qulai.amazon_us.domain.basic.dto.ProductDto;
import com.qulai.amazon_us.service.basic.IProductService;
import com.qulai.common.annotation.Log;
import com.qulai.common.core.controller.BaseController;
import com.qulai.common.core.domain.AjaxResult;
import com.qulai.common.core.page.TableDataInfo;
import com.qulai.common.enums.BusinessType;
import com.qulai.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 商品库存Controller
 *
 * @author hunan
 * @date 2025-01-30
 */
@RestController
@RequestMapping("/basic/product")
public class ProductController extends BaseController {
    @Autowired
    private IProductService productService;


    /**
     * 查询商品库存列表
     */
    @PreAuthorize("@ss.hasPermi('basic:product:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProductDto product) {
        if(product.getParams() !=null && null != product.getParams().get("beginId")) {
            product.setId(null);
        }
        startPage();
        List<Product> list = productService.selectProductList(product);
        for (Product p : list) {
            if ("GG".equals(p.getPlatform()) && (p.getLink()==null || p.getLink().isEmpty())) {
                p.setLink(String.format("https://www.gigab2b.com/index.php?route=product/search&search=%s&search_source=0&search_dimension=1",p.getSku()));
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出商品库存列表
     */
    @PreAuthorize("@ss.hasPermi('basic:product:export')")
    @Log(title = "商品库存", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProductDto product) {
        List<Product> list = productService.selectProductList(product);
        ExcelUtil<Product> util = new ExcelUtil<Product>(Product.class);
        util.exportExcel(response, list, "商品库存数据");
    }

    /**
     * 获取商品库存详细信息
     */
    @PreAuthorize("@ss.hasPermi('basic:product:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(productService.selectProductById(id));
    }

    /**
     * 新增商品库存
     */
    @PreAuthorize("@ss.hasPermi('basic:product:add')")
    @Log(title = "商品库存", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Product product) {
        product.setUpdateBy(getUsername());
        return toAjax(productService.insertProduct(product));
    }

    /**
     * 修改商品库存
     */
    @PreAuthorize("@ss.hasPermi('basic:product:edit')")
    @Log(title = "商品库存", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Product product) {
        product.setUpdateBy(getUsername());
        // 处理 mainImage
        product.setMainImage(truncateAtQuestionMark(product.getMainImage()));
        return toAjax(productService.updateProduct(product));
    }

    // 处理单个字段的公共方法
    private String truncateAtQuestionMark(String url) {
        // 检查 null 或空字符串
        if (url == null || url.isEmpty()) {
            return "";
        }
        // 如果包含“?”，只保留“?”前面的部分
        if (url.contains("?")) {
            return url.substring(0, url.indexOf("?"));
        }
        // 否则保持原样
        return url;
    }

    /**
     * 删除商品库存
     */
    @PreAuthorize("@ss.hasPermi('basic:product:remove')")
    @Log(title = "商品库存", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(productService.deleteProductByIds(ids));
    }




}
